﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

using AlfarEngine;
using AlfarEngine.Components;

namespace EngineTest.GameObjects
{
    class Player : GameObject, ICollidable
    {
        #region Variables

        protected SpotLight mLight;
        protected Sprite mSprite;
        protected Collider mCollider;

        protected float mSpeed;

        #endregion

        //-------------------------------------------

        #region Accessors
        #endregion

        //----------------------------------------------

        #region Methods

        public Player()
            : base("Player")
        {
            Engine.dynamicRoot.AddObject(this);

            mSpeed = 100.0f;

            mLight = AddComponent<SpotLight>();
            mLight.range = 200.0f;
            mLight.angle = 45.0f;

            mSprite = AddComponent<Sprite>();
            mSprite.setTexture("textures/player");
            mSprite.sourceRect = new Rectangle(0, 0, 64, 64);
            mSprite.shadowCaster = false;

            mCollider = AddComponent<Collider>();
            mCollider.dynamic = true;
            mCollider.bounds = new Rectangle(0, 0, 56, 56);
        }

        public override void Update(GameTime pTime)
        {
            // ***------see throught mouse-----***

            MouseState lMouse = Mouse.GetState();

            up = new Vector2(lMouse.X, lMouse.Y) - screenPosition;
            mLight.direction = up;


            // ***------move throught keyboard----***
            KeyboardState lStat = Keyboard.GetState();

            Vector2 lMov = Vector2.Zero;

            if (lStat.IsKeyDown(Keys.Z))
            {
                lMov += -Vector2.UnitY * mSpeed * (float)pTime.ElapsedGameTime.TotalSeconds; 
            }
            else if (lStat.IsKeyDown(Keys.S))
            {
                lMov -= -Vector2.UnitY * mSpeed * (float)pTime.ElapsedGameTime.TotalSeconds;
            }

            if (lStat.IsKeyDown(Keys.Q))
            {
                lMov -= Vector2.UnitX * mSpeed * (float)pTime.ElapsedGameTime.TotalSeconds;
            }
            else if (lStat.IsKeyDown(Keys.D))
            {
                lMov += Vector2.UnitX * mSpeed * (float)pTime.ElapsedGameTime.TotalSeconds;
            }

            if (lMov.LengthSquared() > 1)
                lMov.Normalize();

            position += lMov;

            mDirty = true;

            base.Update(pTime);
        }

        public override void Draw(GameTime pTime, Color pColor)
        {
            base.Draw(pTime, pColor);

            //AlfarEngine.Utilities.Debug.DrawRectangle(mCollider.worldBounds);
        }

        #endregion

        public void OnCollisionEnter(CollisionData pColliders)
        {
            mCollider.MoveTo(mCollider.SlideAlong());
        }

        public void OnCollisionStay(CollisionData pColliders)
        {
            
        }

        public void OnCollisionExit(CollisionData pColliders)
        {

        }
    }
}
